package com.home.duy.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JInternalFrame;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.home.Validation.Validator;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.ThietBiDAO;
import com.home.duy.code.Model.QLBanTableModel;
import com.home.duy.code.Model.QLThietBiTableModel;
import com.home.duy.code.components.ImagePanel;
import com.home.duy.code.components.NumbericTextField;

import javax.swing.JButton;
import com.home.Common.Common;
import com.home.Common.Resources_Duy;
import com.home.Entity.Ban;
import com.home.Entity.ThietBi;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;

public class QuanLyThietBi extends JInternalFrame {

	private QLThietBiTableModel model = null;
	private ButtonGroup btnGroup = null;
	private ThietBi tbSelected = null;
	private TableRowSorter<QLThietBiTableModel> sorter = null;

	private JPanel jContentPane = null;
	private JPanel panelDuLieu = null;
	private JScrollPane scrollPaneTable = null;
	private JTable tableData = null;
	private JPanel panelCommand = null;
	private JButton btnThem = null;
	private JButton btnSua = null;
	private JButton btnXoa = null;
	private JTextField txtFilter = null;
	private JLabel jLabel7 = null;

	private JPanel panelChiTietTB = null;

	private JLabel jLabel = null;

	private JTextField txtTenTB = null;

	private JLabel jLabel1 = null;

	private NumbericTextField txtSoLuong = null;

	private JLabel jLabel2 = null;

	private JRadioButton rdbCo = null;

	private JRadioButton rbdKhong = null;

	private JLabel jLabel3 = null;

	private JScrollPane scrollPaneGC = null;

	private JTextArea txtGhiChu = null;

	private ImagePanel panelComponents = null;

	private JButton btnChapNhan = null;

	private JButton btnHuy = null;

	/**
	 * This is the xxx default constructor
	 */
	public QuanLyThietBi() {
		super();
		btnGroup = new ButtonGroup();
		initialize();
		ThietBi tb = getFirstRecord();
		if (tb != null)
			fillData(tb);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(626, 570);
		this.setContentPane(getJContentPane());
		this.setClosable(true);
		this.setIconifiable(true);

	}

	// =========================== PROCESS METHOD =======================

	private ThietBi getFirstRecord() {
		ThietBi tb = null;
		try {
			ThietBiDAO tbDao = new ThietBiDAO();
			tb = tbDao.findByFirstRecord();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

		return tb;
	}

	// lay thiet bi vua duoc select
	private ThietBi getTbSelected() {
		ThietBi tb = null;
		try {
			int index = getTableData().getSelectedRow();
			if (index < 0) {
				JOptionPane.showMessageDialog(this, "Bạn Hãy Chọn Thiết Bị !",
						"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
				return null;
			}
			ThietBiDAO tbDao = new ThietBiDAO();
			tb = (ThietBi) tbDao.findByPk(ThietBi.class, (Long
					.parseLong((getTableData().getModel().getValueAt(index, 4))
							.toString())));
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
		return tb;
	}

	// dien thong tin vao field
	private void fillData(ThietBi tb) {
		txtTenTB.setText(tb.getTen());
		txtSoLuong.setText(tb.getSoLuong() + "");
		if (tb.isTinhTrang())
			rdbCo.setSelected(true);
		else
			rbdKhong.setSelected(true);
		txtGhiChu.setText(tb.getGhiChu());
	}

	private void processThem() {
		resetAllFields();
		tbSelected = null;
	}

	private void processXoa() {
		try {
			int index = getTableData().getSelectedRow();
			if (index > 0) {
				ThietBi tb = getTbSelected();
				ThietBiDAO tbDao = new ThietBiDAO();
				if (JOptionPane.showConfirmDialog(this,
						"Bạn Có Thực Sự Muốn Xóa Thiết Bị "
								+ tb.getTen().toUpperCase() + " Không ?",
						"Thông Báo", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
					if (tbDao.delete(tb)) {
						JOptionPane.showMessageDialog(this,
								"Đã Xóa Thành Công Thiết Bị " + tb.getTen().toUpperCase()
										+ " !", "Thông Báo",
								JOptionPane.INFORMATION_MESSAGE);
						((AbstractTableModel) getTableData().getModel())
						.fireTableDataChanged();
					}
				}
			} else {
				JOptionPane.showMessageDialog(this, "Bạn Hãy Chọn Thiết Bị !",
						"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void processSua() {
		tbSelected = getTbSelected();
		fillData(tbSelected);
	}

	private void processSaveOrUpdate() {
		try {
			ThietBiDAO tbDao = new ThietBiDAO();
			ThietBi tb = null;
			String thongBao = "";
			if (tbSelected == null) {
				tb = new ThietBi();
				tb.setMaSo(Common.getID());
				thongBao = "Thêm Mới Thiết Bị Thành Công !";
			} else {
				tb = tbSelected;
				thongBao = "Cập Nhật Thiết Bị Thành Công !";
			}

			tb.setTen(txtTenTB.getText().trim());
			tb.setSoLuong(Integer.parseInt(txtSoLuong.getText()));
			if (rdbCo.isSelected())
				tb.setTinhTrang(true);
			else
				tb.setTinhTrang(false);
			tb.setGhiChu(txtGhiChu.getText().trim());

			if (tbDao.saveOrUpdate(tb)) {
				JOptionPane.showMessageDialog(this, thongBao, "Thông Báo",
						JOptionPane.INFORMATION_MESSAGE);
				((AbstractTableModel) getTableData().getModel())
						.fireTableDataChanged();
				if (tbSelected == null) {
					resetAllFields();
				}
			} else {
				JOptionPane.showMessageDialog(this, "Có Lỗi Xảy Ra !", "Lỗi",
						JOptionPane.ERROR_MESSAGE);
			}

		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void locDuLieu() {
		if (Validator.isNotBlank(txtFilter.getText()))
			sorter.setRowFilter(RowFilter.regexFilter(txtFilter.getText()));
		else
			sorter.setRowFilter(null);
	}
	
	private void resetAllFields() {
		txtTenTB.setText("");
		txtTenTB.requestFocus();
		txtSoLuong.setText("");
		rdbCo.setSelected(true);
		txtGhiChu.setText("");
	}

	// ==================================================================

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelDuLieu(), null);
			jContentPane.add(getPanelChiTietTB(), null);
			jContentPane.add(getBtnChapNhan(), null);
			jContentPane.add(getBtnHuy(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes panelDuLieu
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelDuLieu() {
		if (panelDuLieu == null) {
			panelDuLieu = new JPanel();
			panelDuLieu.setLayout(null);
			panelDuLieu.setBounds(new Rectangle(12, 16, 590, 254));
			panelDuLieu.setBorder(BorderFactory
					.createTitledBorder("Danh Sách Bàn"));
			panelDuLieu.add(getScrollPaneTable(), null);
			panelDuLieu.add(getPanelCommand(), null);
		}
		return panelDuLieu;
	}

	/**
	 * This method initializes scrollPaneTable
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneTable() {
		if (scrollPaneTable == null) {
			scrollPaneTable = new JScrollPane();
			scrollPaneTable.setBounds(new Rectangle(12, 51, 566, 188));
			scrollPaneTable.setViewportView(getTableData());
		}
		return scrollPaneTable;
	}

	/**
	 * This method initializes tableData
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTableData() {
		if (tableData == null) {
			tableData = new JTable();
			model = new QLThietBiTableModel();
			tableData.setModel(model);
			
			// sorter
			sorter = new TableRowSorter<QLThietBiTableModel>(model);
			tableData.setRowSorter(sorter);
			
			tableData.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent me) {
					if (me.getClickCount() == 2) {
						processSua();
					}
				}
			});

		}
		return tableData;
	}

	/**
	 * This method initializes panelCommand
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelCommand() {
		if (panelCommand == null) {
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(322, 1, 25, 23));
			jLabel7.setText("");
			jLabel7.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "filter.png"));
			panelCommand = new JPanel();
			panelCommand.setLayout(null);
			panelCommand.setBounds(new Rectangle(12, 26, 565, 27));
			panelCommand.add(getBtnThem(), null);
			panelCommand.add(getBtnSua(), null);
			panelCommand.add(getBtnXoa(), null);
			panelCommand.add(getTxtFilter(), null);
			panelCommand.add(jLabel7, null);
		}
		return panelCommand;
	}

	/**
	 * This method initializes btnThem
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnThem() {
		if (btnThem == null) {
			btnThem = new JButton();
			btnThem.setBounds(new Rectangle(0, 0, 90, 26));
			btnThem.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "add_new.png"));
			btnThem.setText("Thêm");
			btnThem.setContentAreaFilled(false);
			btnThem.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnThem.setContentAreaFilled(true);
					btnThem.setBackground(Color.getColor("#C7D3E0"));
				}

				public void mouseExited(MouseEvent event) {
					btnThem.setContentAreaFilled(false);
				}
			});

			btnThem.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					processThem();
				}
			});
		}

		return btnThem;
	}

	/**
	 * This method initializes btnSua
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnSua() {
		if (btnSua == null) {
			btnSua = new JButton();
			btnSua.setBounds(new Rectangle(90, 0, 85, 26));
			btnSua.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "edit.png"));
			btnSua.setText("Sửa");
			btnSua.setContentAreaFilled(false);
			btnSua.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnSua.setContentAreaFilled(true);
					btnSua.setBackground(Color.getColor("#C7D3E0"));
				}

				public void mouseExited(MouseEvent event) {
					btnSua.setContentAreaFilled(false);
				}
			});
			final QuanLyThietBi qltb = this;
			btnSua.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {

					if (getTableData().getSelectedRow() > 0) {
						processSua();
					} else {
						JOptionPane.showMessageDialog(qltb,
								"Bạn Hãy Chọn Thiết Bị !", "Thông Báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		}
		return btnSua;
	}

	/**
	 * This method initializes btnXoa
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnXoa() {
		if (btnXoa == null) {
			btnXoa = new JButton();
			btnXoa.setBounds(new Rectangle(175, 0, 85, 26));
			btnXoa.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "delete.png"));
			btnXoa.setText("Xóa");
			btnXoa.setContentAreaFilled(false);

			btnXoa.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnXoa.setContentAreaFilled(true);
					btnXoa.setBackground(Color.getColor("#C7D3E0"));
				}

				public void mouseExited(MouseEvent event) {
					btnXoa.setContentAreaFilled(false);
				}
			});
			
			btnXoa.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					processXoa();
				}
			});
		}
		return btnXoa;
	}

	/**
	 * This method initializes txtFilter
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtFilter() {
		if (txtFilter == null) {
			txtFilter = new JTextField();
			txtFilter.setBounds(new Rectangle(347, 0, 217, 26));
			
			txtFilter.getDocument().addDocumentListener(new DocumentListener() {

				@Override
				public void removeUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}

				@Override
				public void insertUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}

				@Override
				public void changedUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}
			});
		}
		return txtFilter;
	}

	/**
	 * This method initializes panelChiTietTB
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelChiTietTB() {
		if (panelChiTietTB == null) {
			jLabel3 = new JLabel();
			jLabel3.setText(" Ghi Chú");
			jLabel3.setFont(jLabel3.getFont().deriveFont(Font.BOLD));
			jLabel3.setBounds(new Rectangle(0, 75, 103, 25));
			jLabel2 = new JLabel();
			jLabel2.setText(" Đang Sử Dụng ");
			jLabel2.setFont(jLabel2.getFont().deriveFont(Font.BOLD));
			jLabel2.setBounds(new Rectangle(0, 50, 105, 25));
			jLabel1 = new JLabel();
			jLabel1.setText(" Số Lượng ");
			jLabel1.setFont(jLabel1.getFont().deriveFont(Font.BOLD));
			jLabel1.setBounds(new Rectangle(0, 25, 108, 25));
			jLabel = new JLabel();
			jLabel.setText(" Tên Thiết Bị");
			jLabel.setFont(jLabel.getFont().deriveFont(Font.BOLD));
			jLabel.setBounds(new Rectangle(0, 0, 106, 25));
			panelChiTietTB = new JPanel();
			panelChiTietTB.setLayout(null);
			panelChiTietTB.setBounds(new Rectangle(12, 274, 590, 195));
			panelChiTietTB.setBorder(BorderFactory
					.createTitledBorder("Chi Tiết Thiết Bị"));
			panelChiTietTB.add(getPanelComponents(), null);
		}
		return panelChiTietTB;
	}

	/**
	 * This method initializes txtTenTB
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtTenTB() {
		if (txtTenTB == null) {
			txtTenTB = new JTextField();
			txtTenTB.setBounds(new Rectangle(120, 0, 220, 26));
		}
		return txtTenTB;
	}

	/**
	 * This method initializes txtSoLuong
	 * 
	 * @return javax.swing.JTextField
	 */
	private NumbericTextField getTxtSoLuong() {
		if (txtSoLuong == null) {
			txtSoLuong = new NumbericTextField("0", 0);
			txtSoLuong.setBounds(new Rectangle(120, 26, 220, 26));

		}
		return txtSoLuong;
	}

	/**
	 * This method initializes rdbCo
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRdbCo() {
		if (rdbCo == null) {
			rdbCo = new JRadioButton();
			rdbCo.setText("Có");
			rdbCo.setBounds(new Rectangle(120, 50, 78, 25));
			btnGroup.add(rdbCo);
		}
		return rdbCo;
	}

	/**
	 * This method initializes rbdKhong
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRbdKhong() {
		if (rbdKhong == null) {
			rbdKhong = new JRadioButton();
			rbdKhong.setText("Không");
			rbdKhong.setBounds(new Rectangle(210, 50, 73, 25));
			btnGroup.add(rbdKhong);
		}
		return rbdKhong;
	}

	/**
	 * This method initializes scrollPaneGC
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneGC() {
		if (scrollPaneGC == null) {
			scrollPaneGC = new JScrollPane();
			scrollPaneGC.setBounds(new Rectangle(120, 75, 444, 75));
			scrollPaneGC.setViewportView(getTxtGhiChu());
		}
		return scrollPaneGC;
	}

	/**
	 * This method initializes txtGhiChu
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTxtGhiChu() {
		if (txtGhiChu == null) {
			txtGhiChu = new JTextArea();
		}
		return txtGhiChu;
	}

	/**
	 * This method initializes panelComponents
	 * 
	 * @return javax.swing.JPanel
	 */
	private ImagePanel getPanelComponents() {
		if (panelComponents == null) {
			panelComponents = new ImagePanel(Resources_Duy.IMAGE_PATH
					+ "bgTBQLTB.png");
			panelComponents.setLayout(null);
			panelComponents.setBounds(new Rectangle(12, 29, 566, 150));
			panelComponents.add(jLabel, null);
			panelComponents.add(getTxtTenTB(), null);
			panelComponents.add(jLabel1, null);
			panelComponents.add(getTxtSoLuong(), null);
			panelComponents.add(jLabel2, null);
			panelComponents.add(getRdbCo(), null);
			panelComponents.add(getRbdKhong(), null);
			panelComponents.add(jLabel3, null);
			panelComponents.add(getScrollPaneGC(), null);
		}
		return panelComponents;
	}

	/**
	 * This method initializes btnChapNhan
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChapNhan() {
		if (btnChapNhan == null) {
			btnChapNhan = new JButton();
			btnChapNhan.setBounds(new Rectangle(401, 470, 100, 25));
			btnChapNhan.setText("Chấp Nhận");
			btnChapNhan.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					processSaveOrUpdate();
				}
			});
		}
		return btnChapNhan;
	}

	/**
	 * This method initializes btnHuy
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnHuy() {
		if (btnHuy == null) {
			btnHuy = new JButton();
			btnHuy.setBounds(new Rectangle(501, 470, 99, 25));
			btnHuy.setText("Hủy");
			final QuanLyThietBi qltb = this;
			btnHuy.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					qltb.dispose();
				}
			});
		}
		return btnHuy;
	}

}
